package com.seamtop.cuber.common.util;


/**
 * Created by zongjunfeng on 2015/9/21.
 */
public class ShortKeyGenerator {
        public static void main(String[] args) {
            String sLongUrl ="http://www.zuidaima.com/share/1550463378934784.htm"; // 3BD768E58042156E54626860E241E999
            String[] aResult = shortUrl(sLongUrl);
            for (int i = 0; i < aResult.length; i++) {
                System.out.println("["+ i +"]:::"+ aResult[i]);
            }
        }

        public static String[] shortUrl(String url) {
            String key ="cuberserver";
            String[] chars = new String[] {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x",
                    "y","z","0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
                    "Q","R","S","T","U","V","W","X","Y","Z"
            };
            String sMD5EncryptResult = (MD5Util.getMD5Code(key + url));
            String hex = sMD5EncryptResult;
            String[] resUrl = new String[4];
            for (int i = 0; i < 4; i++) {
                String sTempSubString = hex.substring(i * 8, i * 8 + 8);
                long lHexLong = 0x3FFFFFFF & Long.parseLong(sTempSubString, 16);
                String outChars ="";
                for (int j = 0; j < 4; j++) {
                    long index = 0x0000003D & lHexLong;
                    outChars += chars[(int) index];
                    lHexLong = lHexLong >> 5;
                }
                resUrl[i] = outChars;
            }
            return resUrl;
        }
}
